%ELEMENTDIV(N,D) Divide two Galois Field elements.
%
%   Inputs:
%
%       N,D     Exponents of the numerator (N) and denominator (D).  
%                The evaluated product is expected to be of the form (a^N)/(a^D)
%
%   Outputs:
%
%       Z       The exponent of the quotient of a^N / a^D.

function q = elementDiv(N,D)

    global m

    % First make sure that N and D are in their reduced forms
    N = elementReduce(N);
    D = elementReduce(D);

    % Check for divide-by-zero error
    if isZero(D)
        error('Divide by zero.');
    end

    % If the numerator is zero, return zero
    if isZero(N)
        q = -1;
        return;
    end

    % If N >= D, then output N - D
    if N >= D
        q = N - D;
    else
        % Add 2^m - 1 to N until N >= D
        while N < D    
            N = N + 2^m - 1;
        end
        % Now return N - D
        q = N - D;
    end
end

